﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommerce.ManagementBase.Common.Base.Common;
using MyCommerce.Presentation.Stores.Presenters;
using MyCommerce.Presentation.Stores.IViews;
using Innova.Microfinanzas.BusinessEntities;

namespace MyCommerce.Windows.Stores.Views
{
    public partial class StoreView : BaseForm, IStore
    {
        private StorePresenter vPresenter;
        private bool isUpdate = false;

        public StoreView()
        {
            InitializeComponent();
            vPresenter = new StorePresenter((IStore)this);
        }

        #region IStore Members

        public List<Store> colStore
        {
            set { gridStore.DataSource = value; }
        }

        public List<Employee> colEmployee
        {
            set
            {
                cmbEmployee.DataSource = value;
                cmbEmployee.DisplayMember = "FullName";
                cmbEmployee.ValueMember = "IdEmployee";
            }
        }

        private Store vobjStore;
        public Store objStore
        {
            get
            {
                vobjStore = new Store();
                if (isUpdate)
                {
                    vobjStore.IdStore = (long)gridStore.ActiveRow.Cells["IdStore"].Value;
                }
                vobjStore.Code = txtCode.Text;
                vobjStore.Name = txtName.Text;
                vobjStore.Address = txtAddress.Text;
                vobjStore.IsCentralStore = checkIsCentral.Checked;
                vobjStore.IsActive = checkIsActive.Checked;
                vobjStore.Phone = txtPhone.Text;
                vobjStore.IdEmployee = (long)cmbEmployee.SelectedValue;
                return vobjStore;
            }
            set
            {
                txtCode.Text = value.Code;
                txtName.Text = value.Name;
                txtAddress.Text = value.Address;
                checkIsCentral.Checked = value.IsCentralStore;
                checkIsActive.Checked = value.IsActive;
                txtPhone.Text = value.Phone;
                if (String.IsNullOrEmpty(cmbEmployee.ValueMember) == false)
                    cmbEmployee.SelectedValue = value.IdEmployee;
            }
        }

        public new event EventHandler OnLoad;

        public event OnSaveEventHandler OnSave;

        public event OnEntityEventHandler OnEdit;

        public event OnEntityEventHandler OnDelete;

        #endregion

        private void StoreView_Load(object sender, EventArgs e)
        {
            OnLoad(sender, e);
            Modify(false);
        }

        private void tsNewButtonToolStrip_Click(object sender, EventArgs e)
        {
            commonButtonsToolStrip1.ToggleButtons();
            Modify(true);
        }

        private void tsEditButtonToolStrip_Click(object sender, EventArgs e)
        {
            OnEdit(sender, (long)gridStore.ActiveRow.Cells["IdStore"].Value);
            isUpdate = true;

            commonButtonsToolStrip1.ToggleButtons();
            Modify(true);
        }

        private void tsDeleteButtonToolStrip_Click(object sender, EventArgs e)
        {
            try
            {
                if (Context.ShowQuestionMessageBox("¿Desea eliminar a este almacen?", "Eliminar almacen") == System.Windows.Forms.DialogResult.Yes)
                {
                    OnDelete(sender, (long)gridStore.ActiveRow.Cells["IdStore"].Value);
                    Context.ShowInformationMessageBox("El almacen se eliminó correctamente.", "Eliminar almacen");
                }   
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
            
        }

        private void tsSaveButtonToolStrip_Click(object sender, EventArgs e)
        {
            try
            {
                if (!DataAutoValidate())
                    return;

                if (!isUpdate)
                {
                    if (OnSave != null)
                    {
                        OnSave(this, true);
                        Context.ShowInformationMessageBox("La información se guardó correctamente.", "Creación/Edición de almacen.");
                    }

                }
                else
                {
                    if (OnSave != null)
                    {
                        OnSave(this, false);
                        Context.ShowInformationMessageBox("La información se actualizó correctamente.", "Creación/Edición de almacen.");
                    }
                }
                isUpdate = false;
                commonButtonsToolStrip1.ToggleButtons();
                Modify(false);
                CleanScreen();
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
            
        }

        private void tsCancelButtonToolStrip_Click(object sender, EventArgs e)
        {
            isUpdate = false;
            commonButtonsToolStrip1.ToggleButtons();
            Modify(false);
            CleanScreen();
        }

        private void tsCloseButtonToolStrip_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private Boolean DataAutoValidate()
        {
            bool result = true;
            StringBuilder message = new StringBuilder();

            if (string.IsNullOrEmpty(txtCode.Text.Trim()))
            {
                result = false;
                message.AppendLine("Debe ingresar un código.");
            }

            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                result = false;
                message.AppendLine("Debe ingresar un nombre.");
            }

            if (!result)
            {
                Context.ShowErrorMessageBox(message.ToString(), "Validación de datos.");
            }

            return result;
        }

        private void Modify(bool edit)
        {
            gridStore.Enabled = !edit;
            txtName.Enabled = edit;
            txtAddress.Enabled = edit;
            checkIsActive.Enabled = edit;
            checkIsCentral.Enabled = edit;
            txtPhone.Enabled = edit;
            txtCode.Enabled = edit;
        }

        private void CleanScreen()
        {
            txtName.Text = string.Empty;
            txtAddress.Text = string.Empty;
            txtPhone.Text = string.Empty;
            checkIsActive.Checked = false;
            checkIsCentral.Checked = false;
            txtCode.Text = string.Empty;
        }
    }
}
